#ifndef _FILEGEO_H_
#define _FILEGEO_H_

/************************************************************************
FileGEO.H
 - Created by Chon Tang (from FileIO.h).   6/14/2001.
 - Modified by Chon Tang.  New buffer size.  3/1/03.

 TERN, Inc.
 All rights reserved.

********************** DISCLAIMER *********************************
This library is not to be used in situations where improper operation
or improper use could cause loss of life, personal injury or property damage.
***********************************************************************/

#define SECTORSIZE  0x200U  // Number of bytes in a PC Card sector, 512.

/****
3/1/03: The follow MACROS have been obsoleted.  There's no need to restrict
size of clusters supported artificially.  User application can always check
size of geometry before attempting to open file.

// Increasing SPCMAX increases memory footprint, but allows larger disks.
// SPCMAX should be set to the largest sectors per cluster expected for
//   the disks that are being used.  Run CHKDSK x: in DOS to determine
//   allocation unit size, divide by 512 (=1/2KB) to get sectors per cluster.

****/
#define SPCMAX 16U  // DEFAULT maximum Sectors Per Cluster that can be handled.
#define ff_BUFFER_SIZE  SECTORSIZE*SPCMAX // Size of cluster memory buffer,
                                  //  some multiple of sectors.  See gTOO_BIG.
/**** END 3/1/03 *****/


/* Geometry error codes. */
/* Return values for checking status of initial disk operations. */

#define gOK             0x10 // Disk geometry status is OK.
#define gUNRECOGNIZABLE 0x11 // Disk geometry cannot be determined.
#define gTOO_BIG        0x12 // Pre-assigned cluster buffer sizes too small for
                             // this particular disk format.
#define gWRONGFAT       0x13 // The FAT is not of a type this code handles.
#define gODDGEO         0x14 // The geometry of this disk is unusual; reformat.
#define gDISKBUSY       0x15 // Disk is probably not inserted, reports as busy.
#define gWRITE_ERROR    0x16 // A low-level write operation failed.
#define gREAD_ERROR     0x17 // A low-level Read operation failed.

// The disk geometry structure, used to pass information about the location
//  of the FATs, boot sector, data sector, maximum capacity, etc.
//  initPCFlash reads the geometry and passes it out via ffgeo.
//  This information will not change unless disk is removed.
// The disk is read and written by sectors of 512 bytes.
// Data is stored in clusters that take up some power of 2 sectors, see SPCMAX.
struct ff_geo {
  char g_version[20]; // Used to keep track of changes in code, set in FileIO.c.
  unsigned int g_SectorsPerCluster, // Number of sectors in each cluster,
					//  see SPCMAX.
	       g_ClusterSize,	// Size in bytes of each cluster.
	       g_SectorsPerFAT, // Number of sectors in each FAT.
	       g_MaxDirEntries, // Maximum directory entries available.
          g_MaxClusters,	// Total available clusters for data.
	       g_EOFFATEntry,	// Value used to make End of File in FAT.
	       g_DiskStatus;	// Used to report errors in initializing the disk.
  char g_FATType;		// '2' for 12 bit FAT, '6' for 16 bit FAT.
  unsigned long
		g_FAT1Sector, g_FAT2Sector,  // Addresses of FAT on disk.
		g_DirSector,  // Address of directory sector on disk.
		g_DataStartSector,  // Address where data section starts.
		g_BootSector,   // Address for boot sector,
		g_TotalSectors, // Total available sectors for data.
		g_Capacity,	// Total available bytes for data.
		g_Reserved;	// Number of reserved sectors.
};

/* MBR Partition structure */
struct ff_partition
{
	unsigned char g_state; /* 00h inactive, 80h active. */
   unsigned char g_begHead; /* Head address for beginning sector. */
   unsigned int g_begCyl;  /* Cylinder/Sector for beginning */

	unsigned char g_type;
   	/* 00h Unknown or Nothing
			01h 12-bit FAT
			04h 16-bit FAT (Partition Smaller than 32MB)
			05h Extended MS-DOS Partition
			06h 16-bit FAT (Partition Larger than 32MB)
			0Bh 32-bit FAT (Partition Up to 2048GB)
			0Ch Same as 0BH, but uses LBA1 13h Extensions
			0Eh Same as 06H, but uses LBA1 13h Extensions
         0Fh Same as 05H, but uses LBA1 13h Extensions
			00h unknown or nothing
      */

	unsigned char g_endHead;
   unsigned int g_endCyl;
   unsigned long g_lbaOff;     /* Sector offset from MBR to partition. */
   unsigned long g_numSectors; /* Number of sectors in partition. */
};

struct ff_geo* fs_GetGeometry(void);

#endif /* _FILEGEO_H_ */
